import pygtk
import gtk
import gtk.glade
import gobject


class tela:
	def setopenfile(self, widget):
		self.filename = widget.get_filename()
		print self.filename
		widget.hide()
		f = open(self.filename, "r")
		self.textview.get_buffer().set_text(f.read())
		f.close()
		
	def showopenfile(self, widget):
		self.winopen = self.gladeobj.get_widget('fileopen')
		self.winopen.show()
		self.winopen.connect('file-activated', self.setopenfile)
		self.winopen.connect('destroy', self.setopenfile)
	
	def compilar(self, widget):	
		print "Lendo Arquivo " + self.filename
		f= open(self.filename, "r")
		self.kernel.start()
		numlin=1
		for i in f.readlines():
			self.kernel.analisar_linha(i, numlin)
			numlin+=1
		#self.kernel.mostra_tab("tab")
		print self.kernel.tab_simbol
		for i in self.kernel.tab_simbol:
			print "1:" + str(i[0])
			print "2:" + str(i[1])
			numlin+=1
			if not(str(i[1]) == " "):
				iter=self.model.append(["foo", "bar"]) 
								
	
	def __init__(self):
		#self.kernel = anlex.analisex()
		self.gladeobj = gtk.glade.XML("compiler.glade")
		self.win = self.gladeobj.get_widget('frmcompilar')
		self.openfilebtn = self.gladeobj.get_widget('imagemenuitem2')
		self.quitbtn = self.gladeobj.get_widget('imagemenuitem5')
		
		self.compilebtn = self.gladeobj.get_widget('menuitem3')
		self.textview = self.gladeobj.get_widget('textview1')
		self.treeview = self.gladeobj.get_widget('treeview1')
		col1 = gtk.TreeViewColumn ('Tipo', gtk.CellRendererText(), text=1)
		col2 = gtk.TreeViewColumn ('Valor', gtk.CellRendererText(), text=1)
		self.model = gtk.ListStore (gobject.TYPE_STRING, gobject.TYPE_STRING)
		self.treeview.set_headers_visible (1)
		self.treeview.append_column(col1)
		self.treeview.append_column(col2)
		self.treeview.set_model(self.model)

		self.openfilebtn.connect('activate', self.showopenfile)
		self.compilebtn.connect('activate', self.compilar)
		self.quitbtn.connect('activate', gtk.main_quit)
		self.openfilebtn.text ="as"
		#dic = {'on_imagemenuitem1_activate':self.openfile}
		#self.gladeobj.signal_autoconnect(dic)
		
		self.win.connect('destroy',gtk.main_quit)
		self.win.show()
if __name__ == "__main__":
	a =tela()
	gtk.main()

